package com.briup.cms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;



import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.bean.basic.Slideshow;
import com.briup.cms.dao.basic.SlideshowMapper;
import com.briup.cms.service.ISlideshowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.util.Assert;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.List;

import static com.briup.cms.util.ResultCode.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lining
 * @since 2024-09-10
 */
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow> implements ISlideshowService {
    @Autowired
    private SlideshowMapper slideshowMapper;

    @Override
    public Page<Slideshow> findByPage(String desc, Integer pageNum, Integer pageSize, String status){
        Page<Slideshow> pageParam = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Slideshow> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(desc),Slideshow::getDescription,desc);
        Page<Slideshow> page = slideshowMapper.selectPage(pageParam, null);
        return page;
    }

    @Override
    public List<Slideshow> findAll() {
        return slideshowMapper.selectList(null);
    }

    @Override
    public Slideshow findById(Integer id) {
        return slideshowMapper.selectById(id);
    }

    @Override
    public void deleteByIds(String ids) {
        slideshowMapper.deleteById(ids);
    }

    @Override
    public void saveOrUpdate(String description, Integer id, String status, String url) {
        LambdaQueryWrapper<Slideshow> urlWrapper = new LambdaQueryWrapper<>();
        urlWrapper.eq(Slideshow::getUrl,url);
        Slideshow slideshow = slideshowMapper.selectOne(urlWrapper);
        Slideshow s = new Slideshow()
                .setId(id)
                .setDescription(description)
                .setUrl(url)
                .setStatus(status)
                .setUploadTime(LocalDateTime.now());
        if(id == null){
            //新增
            Assert.notNull(slideshow,URL_EXIT);
            slideshowMapper.insert(s);
        }else{
            //修改
            LambdaQueryWrapper<Slideshow> idWrapper = new LambdaQueryWrapper<>();
            Slideshow slideshow1 = slideshowMapper.selectOne(idWrapper.eq(Slideshow::getId, id));
            Assert.isNull(slideshow1,ID_NOT_EXIT);
            if(slideshow1.getUrl().equals(url) || slideshow ==null){
                slideshowMapper.updateById(s);
            }

        }
    }

}
